<template>
  <a-modal
    title="操作"
    :width="600"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item label="父级ID">
          <a-input v-decorator="['title', {}]" disabled/>
        </a-form-item>

        <a-form-item label="文件夹名称">
          <a-input v-decorator="['fileManager.fileName', {}]"/>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import { saveFileManager } from "@/api/manage";

export default {
  name: "OrgModal",
  data() {
    return {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      visible: false,
      confirmLoading: false,
      mdl: {}
    };
  },
  beforeCreate() {
    this.form = this.$form.createForm(this);
  },
  created() {},
  methods: {
    add(item) {
      this.edit(item);
    },
    edit(record) {
      const data = { title: record.title };
      this.mdl = Object.assign({}, record);
      this.visible = true;
      this.$nextTick(() => {
        this.form.setFieldsValue({ ...data });
      });
    },
    close() {
      this.$emit("close");
      this.visible = false;
    },
    handleOk() {
      const _this = this;
      // 触发表单验证
      this.form.validateFields((err, values) => {
        // 验证表单没错误
        if (!err) {
          console.log(_this.mdl.id);
          values.fileManager.parentFileManager = {};
          values.fileManager.parentFileManager.id = _this.mdl.id;
          let fileManager = JSON.stringify(values.fileManager);
          saveFileManager(fileManager).then(res => {
            console.log(res);
          });
          _this.$message.success("保存成功");
          _this.$emit("ok");
          _this.confirmLoading = false;
          _this.close();
        }
      });
    },
    handleCancel() {
      this.close();
    }
  }
};
</script>
